import ftWebSocket from "futu-api/main.js";
import proto from "futu-api/proto";
import moment from 'moment';
const QotMarket = proto.Qot_Common.QotMarket;
const SubType = proto.Qot_Common.SubType;
const RehabType = proto.Qot_Common.RehabType;
const KLType = proto.Qot_Common.KLType;
const RetType = proto.Common.RetType;

class FutuSource {
    constructor(ready) {
        this.websocket = new ftWebSocket();
        this.websocket.start("stock.deployment.cn", 13569, true, null);
        this.websocket.onlogin = ready
    }

    async geKLines(code, period) {
        const websocket = this.websocket;

        let klType = KLType.KLType_Day;
        let subType = SubType.SubType_KL_Day;
        if(period == "15min"){
            klType = KLType.KLType_15Min
            subType = SubType.SubType_KL_15Min
        } else if(period == "30min"){
            klType = KLType.KLType_30Min
            subType = SubType.SubType_KL_30Min
        } else if(period == "1h"){
            klType = KLType.KLType_60Min
            subType = SubType.SubType_KL_60Min
        } else if(period == "1w") {
            klType = KLType.KLType_Week
            subType = SubType.SubType_KL_Week
        } else if(period == "1M") {
            klType = KLType.KLType_Month
            subType = SubType.SubType_KL_Month
        }
        const market = code.endsWith(".SH")?QotMarket.QotMarket_CNSH_Security: QotMarket.QotMarket_CNSZ_Security;
        const cd = code.replace(".SH", "").replace(".SZ", "");
        console.log("code", code, "market", market, "cd", cd, "klType", klType)



       const subres =  await websocket.Sub({
            c2s: {
                securityList: [
                    {
                        market: market,
                        code: cd
                    },
                ],
                subTypeList: [ subType], // 订阅实时K线(分K)类型
                isSubOrUnSub: true, // 订阅 true, 反订阅 false
                isRegOrUnRegPush: true, // 注册推送 true, 反注册推送 false
            },
        })
        console.log("subres", subres)

        const substatreq = {
            c2s: {
                isReqAllConn: true,
            },
        }; // 获取订阅状态参数

        const substatres = await websocket.GetSubInfo(substatreq)
        if(substatres.retType == RetType.RetType_Succeed){
            console.log("GetSubInfo: errCode %d, retMsg %s, retType %d", substatres.errCode, substatres.retMsg, substatres.retType);
            console.log("GetSubInfo:", JSON.stringify(substatres.s2c));
        } else {
            console.log("GetSubInfo: error")
        }
        const klreq = {
            c2s: {
                rehabType: RehabType.RehabType_Forward,
                klType: klType,
                security: {
                    market: market,
                    code: cd
                },
                reqNum: 1000,
            },
        };


        const linesRes = await  websocket.GetKL(klreq)
        let { errCode, retMsg, retType,s2c } = linesRes
        // console.log("KL: errCode %d, retMsg %s, retType %d", errCode, retMsg, retType);
        if(retType == RetType.RetType_Succeed){
            let data = s2c;

            // var list = JSON.parse(JSON.stringify(data.klList));
            var list = [];
            for(var item of data.klList){
                var tmstp = moment(item.time, "YYYY-MM-DD HH:mm:ss").unix() * 1000
                list.push({
                    time: tmstp,
                    timestamp: tmstp,
                    close: item.closePrice,
                    open: item.openPrice,
                    high: item.highPrice,
                    low: item.lowPrice,
                    volume: item.volume / 10000,
                    amount: item.turnover,
                })
            }
            return list;
        }


    }
}
export default FutuSource;
